package com.jg.visitor.controller.module.visitor;

import com.jg.visitor.common.bean.DaySettingBean;
import com.jg.visitor.common.bean.PageDTO;
import com.jg.visitor.common.bean.ResponseBean;
import com.jg.visitor.common.utils.CheckParamUtils;
import com.jg.visitor.config.exception.BaseException;
import com.jg.visitor.controller.dto.*;
import com.jg.visitor.controller.vo.VisitorApplyRecordVO;
import com.jg.visitor.service.GlobalConfigService;
import com.jg.visitor.service.UserService;
import com.jg.visitor.service.VisitorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "访客接口")
@RestController
@RequestMapping("/api/visitor")
public class VisitorController {

    @Resource
    private VisitorInfoService visitorInfoService;
    @Resource
    private UserService userService;
    @Resource
    private GlobalConfigService globalConfigService;

    @GetMapping("/select-visitor-info")
    @ApiOperation("查询当前接入访客基本信息接口")
    public ResponseBean selectVisitorInfo(){
        VisitorInfoDTO dto = visitorInfoService.selectLoginVisitorInfo();
        return ResponseBean.success(dto);
    }

    @PostMapping("/add-edit-visitor-info")
    @ApiOperation("新增或编辑访客信息")
    public ResponseBean addOrEditUserInfo(@RequestBody VisitorInfoDTO dto){
        if(CheckParamUtils.isAnyStringEmpty(dto.getUsername(),dto.getCardNumber(),
                dto.getVisitorCompany(),dto.getAddressDetail())){
            throw new BaseException(-1,"信息不全,请检查！");
        }

        VisitorInfoDTO visitorInfoDTO = visitorInfoService.addOrEditVisitorInfo(dto);

        return ResponseBean.success(visitorInfoDTO);
    }

    @PostMapping("/add-approve-record")
    @ApiOperation("访客新增申请单")
    public ResponseBean addApproveRecord(@RequestBody VisitorApplyRecordVO dto){

        visitorInfoService.addApproveRecord(dto);

        return ResponseBean.success();
    }

    @PostMapping("/cancel-approve-record/{recordId}")
    @ApiOperation("访客取消申请单")
    public ResponseBean cancelApproveRecord(@PathVariable Long recordId){

        visitorInfoService.cancelApproveRecord(recordId);

        return ResponseBean.success();
    }

    @PostMapping("/visitor-can-add-record")
    @ApiOperation("校验是否还能继续申请访问")
    public ResponseBean visitorCanAddRecord(){

        boolean flag = visitorInfoService.visitorCanAddRecord();

        return ResponseBean.success(flag);
    }

    @PostMapping("/select-self-record/{page}/{pageSize}")
    @ApiOperation("分页查询访客申请单")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "每页查询数量",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "status",value = "状态 0全部1待审核2已审核",dataTypeClass = Integer.class)})
    public ResponseBean selectSelfRecord(@PathVariable Integer page, @PathVariable Integer pageSize,
                                         @RequestParam("status") Integer status){

        PageDTO<ApproverApproveRecordDTO> data = visitorInfoService.selectSelfRecord(page,pageSize,status);

        return ResponseBean.success(data);
    }

    @ApiOperation("被访者手机号验证")
    @GetMapping("/check-phone/{phone}")
    @ApiImplicitParam(name = "phone",value = "手机号",dataTypeClass = String.class)
    public ResponseBean phone(@PathVariable String phone) {

        boolean phoneIsOk = userService.getTel(phone);

        return ResponseBean.success(phoneIsOk);
    }

    @ApiOperation("获取访问时间设置")
    @GetMapping("/select-max-config-time")
    public ResponseBean selectMaxConfigTime() {
        DaySettingBean maxTime = userService.selectMaxConfigTime();

        return ResponseBean.success(maxTime);
    }

    @ApiOperation("访客身份证解绑")
    @GetMapping("/visitor-id-card-unbound")
    public ResponseBean idCardUnbound() {
        boolean success = visitorInfoService.idCardUnbound();

        return ResponseBean.success(success);
    }

    @ApiOperation("访客申请通过后查询自己通行码")
    @GetMapping("/visitor-get-qr-code")
    public ResponseBean getQRCode() {
        String qrCode = visitorInfoService.getQRCode();

        return ResponseBean.success(qrCode);
    }

    @ApiOperation("访客查询进出记录")
    @GetMapping("/select-out-in-history/{page}/{pageSize}")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "每页查询数量",dataTypeClass = Integer.class)})
    public ResponseBean selectOutInHistory(@PathVariable Integer page, @PathVariable Integer pageSize) {
        PageDTO<OutInHistoryDTO> qrCode = visitorInfoService.selectOutInHistory(page,pageSize);

        return ResponseBean.success(qrCode);
    }

    @ApiOperation("访客查询核酸报告字段")
    @GetMapping("/select-nucleic-show")
    public ResponseBean selectNucleicShow(){
        String report = globalConfigService.selectNucleicShow();

        return ResponseBean.success(report);
    }
}
